package com.example.a80205050.myapplication.Dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import com.example.a80205050.myapplication.Modle.Tb_outaccount;

/**
 * Created by 80205050 on 2017/10/30.
 */

public class OutaccountDao {
    private DBOpenHelper helper;//创建DBOpenHelper对象
    private SQLiteDatabase db;//创建SQLiteDataBase对象
    public OutaccountDao(Context context) {
        helper=new DBOpenHelper(context);
    }
    /**
     * 添加支出信息
     * @param tb_outaccount
     */
    public void add(Tb_outaccount tb_outaccount){
        db=helper.getWritableDatabase();//初始化SQLiteDatabase
        db.execSQL("insert into  tb_outaccount(_id,money,time,type,address,mark) values(?,?,?,?,?,?)",
                new Object[]{tb_outaccount.get_id(),tb_outaccount.getMoney(),tb_outaccount.getTime(),
                        tb_outaccount.getType(),tb_outaccount.getAddress(),tb_outaccount.getMark()});
    }

    /**
     * 更新数据
     * @param tb_outaccount
     */
    public void update(Tb_outaccount tb_outaccount){
        db=helper.getWritableDatabase();//初始化SQLiteDatabase
        db.execSQL("update tb_outaccount set money=?,time=?,type=?,address=?,mark=? where _id=?",
                new Object[]{tb_outaccount.getMoney(),tb_outaccount.getTime(),
                        tb_outaccount.getType(),tb_outaccount.getAddress(),tb_outaccount.getMark(),tb_outaccount.get_id()});
    }

    /**
     * 根据id查找支出的数据
     * @param id
     * @return
     */
    public Tb_outaccount find(int id){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select _id,money,time,type,address,mark from tb_outaccount " +
                "where _id=?",new String[]{String.valueOf(id)});
        if(cursor.moveToNext()){
            return new Tb_outaccount(cursor.getInt(cursor.getColumnIndex("_id")),
                    cursor.getDouble(cursor.getColumnIndex("money")),
                    cursor.getString(cursor.getColumnIndex("time")),
                    cursor.getString(cursor.getColumnIndex("type")),
                    cursor.getString(cursor.getColumnIndex("address")),
                    cursor.getString(cursor.getColumnIndex("mark"))
            );
        }
        return null;
    }

    /**
     * 删除支出的信息
     * @param ids
     */
    public void delete(Integer... ids){
        if(ids.length>0){
            StringBuilder sb=new StringBuilder();
            for(int i=0;i<ids.length;i++){
                sb.append('?').append(',');
            }
            sb.deleteCharAt(sb.length()-1);
            db=helper.getWritableDatabase();
            db.execSQL("delete from tb_outaccount where _id in ("+sb+")",(Object[])ids);
        }
    }

    /**
     * 获取收入的信息
     * @param start 初始的位置
     * @param count  每页显示的数量
     * @return
     */
    public List<Tb_outaccount> getScrollData(int start,int count){
        List<Tb_outaccount> tb_outaccountList=new ArrayList<Tb_outaccount>();//初始化集合对象
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from tb_outaccount limit ?,?",new String[]{
                String.valueOf(start),String.valueOf(count)
        });
        while (cursor.moveToNext()){
            tb_outaccountList.add(new Tb_outaccount(
                    cursor.getInt(cursor.getColumnIndex("_id")),
                    cursor.getDouble(cursor.getColumnIndex("money")),
                    cursor.getString(cursor.getColumnIndex("time")),
                    cursor.getString(cursor.getColumnIndex("type")),
                    cursor.getString(cursor.getColumnIndex("address")),
                    cursor.getString(cursor.getColumnIndex("mark"))
            ));
        }
        return tb_outaccountList;
    }

    /**
     * 获取总的记录数
     * @return
     */
    public long getCount(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select count(_id) from tb_outaccount ",null);
        if(cursor.moveToNext()){
            return cursor.getLong(0);
        }
        return 0;
    }
    /**
     * 返回收入的最大编号
     * @return
     */
    public int getMaxId(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select max(_id) from tb_outaccount ",null);
        if(cursor.moveToLast()){
            return cursor.getInt(0);
        }
        return 0;//没有数据，返回0
    }



}
